from flask import jsonify

from commons.entity.enum.response_code_enum import ResponseCodeEnum
from commons.entity.vo.response_vo import ResponseVO


class BaseController:
    STATIC_SUCCESS = "success"
    STATIC_ERROR = "error"

    def get_success_response(self, data=None):
        response_vo = ResponseVO()
        response_vo.status = self.STATIC_SUCCESS
        response_vo.code = ResponseCodeEnum.CODE_200.code
        response_vo.info = ResponseCodeEnum.CODE_200.msg
        response_vo.data = None
        if data:
            response_vo.data = data.to_dict()
        return jsonify(response_vo.to_dict())

    def get_business_error_response(self, exception, data=None):
        response_vo = ResponseVO()
        response_vo.status = self.STATIC_ERROR
        response_vo.code = exception.code if exception.code else ResponseCodeEnum.CODE_600.code
        response_vo.info = str(exception)
        response_vo.data = data.to_dict()
        return jsonify(response_vo.to_dict())

    def get_server_error_response(self, data=None):
        response_vo = ResponseVO()
        response_vo.status = self.STATIC_ERROR
        response_vo.code = ResponseCodeEnum.CODE_500.code
        response_vo.info = ResponseCodeEnum.CODE_500.msg
        response_vo.data = data.to_dict()
        return jsonify(response_vo.to_dict())
